package org.example.sdk;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import retrofit2.Converter;
import retrofit2.Retrofit;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

class JsonNodeConverterFactory extends Converter.Factory {

    private static final JsonNodeConverter jsonNodeConverter = new JsonNodeConverter();

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(
            @NotNull Type type, @NotNull Annotation[] annotations, @NotNull Retrofit retrofit) {
        return jsonNodeConverter;
    }

    private static class JsonNodeConverter implements Converter<ResponseBody, Object> {

        private static final ObjectMapper objectMapper = new ObjectMapper();

        @Override
        public Object convert(ResponseBody responseBody) throws IOException {
            return objectMapper.readTree(responseBody.byteStream());
        }
    }
}
